package bman.game.battletank;

import java.util.List;
import java.util.logging.Logger;

import bman.game.battletank.BattleTank.Types;
import bman.game.engine.Entity;
import bman.game.engine.GameMap;
import bman.game.engine.GraphicsEngine;
import bman.game.engine.Timer;

/**
 * This class will remove unwanted Entities from the Map. It's meant to be a fail safe
 * in case the game logic fails to remove the entity.
 * 
 * @author MrJacky
 *
 */
public class GarbageCollector extends Thread
{
	Logger log = Logger.getLogger(this.getClass().getName());
	
	GameMap map;
	
	public GarbageCollector(GameMap map)
	{
		this.map = map;
		start();
	}
	
	public void run()
	{
		boolean run = false;
		Timer t = GraphicsEngine.getTimer();
		
		log.info("Starting GarbageCollector...");
		
		while(run)
		{
			try
			{
				t.update();
				
				List l = map.getEntities();
				for(int i = 0; i < l.size();i++)
				{
					Entity e = (Entity)l.get(i);
					
					if(e.getType().equalsIgnoreCase(Types.EXPLOSION) ||
						e.isActive() == false)
					{
						map.remove(e);
					}
				}
				sleep(t.getSleep()*10);
			}
			catch(Exception e)
			{
				
			}
		}
	}
	

}
